package com.ks.api.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ks.api.dto.KsLoginDTO;
import com.ks.api.entity.CultivationLevel;
import com.ks.api.entity.KsUser;
import com.ks.api.service.CultivationLevelService;
import com.ks.api.service.KsUserService;

@RestController
@RequestMapping("/user")
public class KsUserController {
    
    @Autowired
    private KsUserService ksUserService;
    
    @Autowired
    private CultivationLevelService cultivationLevelService;
    
    /**
     * 管理后台登录
     */
    @PostMapping("/admin/login")
    public ResponseEntity<Map<String, Object>> adminLogin(@RequestParam String username, @RequestParam String password) {
        Map<String, Object> response = new HashMap<>();
        
        // 验证用户名和密码
        if ("admin".equals(username) && "ly_zhitou_password_123".equals(password)) {
            response.put("success", true);
            response.put("message", "登录成功");
            return ResponseEntity.ok(response);
        } else {
            response.put("success", false);
            response.put("message", "用户名或密码错误");
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(response);
        }
    }
    
    /**
     * 用户登录
     */
    @PostMapping("/login")
    public ResponseEntity<Map<String, Object>> login(@RequestBody KsLoginDTO loginDTO) {
        KsUser user = ksUserService.login(loginDTO);
        
        // 获取用户当前修炼等级
        CultivationLevel currentLevel = cultivationLevelService.getCurrentLevel(user.getCultivationScore());
        
        Map<String, Object> result = new HashMap<>();
        result.put("user", user);
        result.put("currentLevel", currentLevel);
        
        return ResponseEntity.ok(result);
    }
    
    /**
     * 获取用户信息
     */
    @GetMapping("/info")
    public ResponseEntity<KsUser> getUserInfo(@RequestParam String openId) {
        KsUser user = ksUserService.findByOpenId(openId);
        if (user != null) {
            return ResponseEntity.ok(user);
        }
        return ResponseEntity.notFound().build();
    }

    /**
     * 更新用户积分
     */
    @PostMapping("/update_user")
    public ResponseEntity<KsUser> updateScore(@RequestBody KsUser user) {
        ksUserService.updateUserById(user);
        return ResponseEntity.ok(user);
    }


    // 查询用户现在的阶段是什么 用户信息 下一个阶段是什么 大阶段信息展示
    @GetMapping("/getUserInfoAndLevel")
    public ResponseEntity<Map<String,Object>> getUserInfoAndLevel(@RequestParam Integer userId) {
        return ResponseEntity.ok(ksUserService.getUserInfoAndLevel(userId));
    }
}